<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/1
 * Time: 上午9:09
 */
class Login extends CI_Controller
{
    public function index(){
        $has_error = 0;
        $post_data = $this->input->post();
        if($post_data){
//            print_r($post_data);
            if(empty($post_data['phone'])){
                $this->smarty->assign("phone_err", "请填写手机号");
                $has_error = 1;
            }
            if(empty($post_data['pwd'])){
                $this->smarty->assign("pwd_err", "请填写密码");
                $has_error = 1;
            }
            if($has_error==0){
                //判断手机号是否存在
                $this->load->model('User_model');
                $user = $this->User_model->get_user_by_phone_or_username($post_data['phone']);
                if(empty($user)){
                    $this->smarty->assign("phone_err", "手机号不存在");
                    $this->smarty->assign("err_msg", "手机号不存在");
                    $has_error = 1;
                }else{
                  $password = md5(md5($post_data['pwd']) . $user['rand_str']);

                    if($user['password']!=$password){
                        $this->smarty->assign("pwd_err", "密码错误");
                        $this->smarty->assign("err_msg", "密码错误");
                    }else{
                        //获取老师身份信息
                        $this->load->model('Teachers_model');
                        $teacher = $this->Teachers_model->get_teacher_by_user_id($user['id']);
                        if(empty($teacher)){
                          $this->smarty->assign("phone_err", "手机号不存在");
                          $this->smarty->assign("err_msg", "手机号不存在");
                          $has_error = 1;
                        }else{
                          //获取学校身份
                          $this->load->model('Schoolo2o_model');
                          $school = $this->Schoolo2o_model->get_school_by_id($teacher['school_id']);
                          
                          $agent_info = get_field_by_where("agents", "*", "school_id = '{$teacher['school_id']}'");
                          if(empty($school)) {
                            $this->smarty->assign("phone_err", "学校不存在");
                            $this->smarty->assign("err_msg", "学校不存在");
                            $has_error = 1;
                          }else{
                            //设置cookie
                            $user_data = array(
                              'id' => $user['id'],
                              'head_image' => $teacher['header_img'],
                              'user_name' => $teacher['teacher_name'],
                              'teacher_id' => $teacher['id'],
                              'school_name' => $school['school_name'],
                              'school_prefix' => $school['prefix'],
                              'school_domain' => $school['url_school'],
                              'school_id' => $teacher['school_id'],
                              'link_tel' => $school['link_tel'],
                              'mobile' => $user['mobile'],
                              'is_manager' => $teacher['is_manager'],
                              'wx_mp_qrcode' => $school['wx_mp_qrcode'],
                              'agent' => !empty($agent_info['id']) ? $agent_info['id'] : 0,
                              'logged_in' => 1);
                            $this->session->set_userdata('user_data', $user_data);
                          }
                        }
                          
                        if($has_error == 0){
                          if($school['item_num'] <= 10){
                            echo "<script> alert('您的视频课账户数量仅剩{$school['item_num']}个，请尽快升级套餐');parent.location.href='/';  </script>";
                          }else{
                            header("Location:/");exit;
                          }
                        }

                    }
                }
            }

        }
        $this->smarty->display("login.html");
    }


    function logout(){
        unset(
            $_SESSION['id'],
            $_SESSION['user_head'],
            $_SESSION['user_name'],
            $_SESSION['user_phone'],
            $_SESSION['logged_in']
        );
        header("Location:/login");exit;
    }
}
